<template>
  <div class="forgot-password-container">
    <div class="top-text">找回密码</div>
    <div class="forgot-password-box">
      <h3 class="forgot-password-title">更新密码</h3>
      <el-form :model="forgotPasswordForm" ref="forgotPasswordFormRef" label-width="120px">
        <el-form-item label="登录名" prop="username" :rules="[{ required: true, message: '请输入登录名', trigger: 'blur' }]">
          <el-input type="text" v-model="forgotPasswordForm.username" size="small"></el-input>
        </el-form-item>
        <el-form-item label="旧密码" prop="oldPassword" :rules="[{ required: true, message: '请输入旧密码', trigger: 'blur' }]">
          <el-input type="password" v-model="forgotPasswordForm.oldPassword" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword" :rules="[{ required: true, message: '请输入新密码', trigger: 'blur' }]">
          <el-input type="password" v-model="forgotPasswordForm.newPassword" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmNewPassword" :rules="[
          { required: true, message: '请确认新密码', trigger: 'blur' },
          { validator: validateNewPassword, trigger: 'blur' }
        ]">
          <el-input type="password" v-model="forgotPasswordForm.confirmNewPassword" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="forgot-password-button" type="primary" @click="resetPassword">更新密码</el-button>
        </el-form-item>
      </el-form>
      <div class="error-message">{{ errorMsg }}</div>
      <div class="extra-buttons">
        <el-button type="text" @click="goToLogin">返回登录</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import router from "@/util/router";
import http from '@/axios/http';

const forgotPasswordForm = reactive({
  username: '',
  oldPassword: '',
  newPassword: '',
  confirmNewPassword: ''
});
const forgotPasswordFormRef = ref(null); // 用于表单验证
const errorMsg = ref(''); // 使用 ref 创建响应式变量

// 验证两次新密码是否一致
function validateNewPassword(rule, value, callback) {
  if (value !== forgotPasswordForm.newPassword) {
    callback(new Error('两次输入的新密码不一致'));
  } else {
    callback();
  }
}

// 更新密码逻辑
function resetPassword() {
  // 验证表单
  forgotPasswordFormRef.value.validate((valid) => {
    if (!valid) {
      return; // 如果验证不通过，直接返回
    }

    const resetData = {
      username: forgotPasswordForm.username,
      oldPassword: forgotPasswordForm.oldPassword,
      newPassword: forgotPasswordForm.newPassword
    };

    http.post('/lyy/resetPassword', resetData).then((res) => {
      let result = res.data; // 处理器返还的结果数据

      if (result.result === "success") { // 返还标志是 "success"
        router.push('/login');
      } else {
        errorMsg.value = result.error || "更新密码失败，请稍后再试";
      }
    }).catch((error) => {
      console.error("更新密码请求失败", error);
      errorMsg.value = "更新密码请求失败，请稍后再试";
    });
  });
}

// 跳转到登录页面
function goToLogin() {
  router.push('/login');
}
</script>

<style scoped>
@import '@/styles/forgotPasswordStyles.css';
</style>